package com.zhi.mbtibot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhi.mbtibot.entity.User;
import com.zhi.mbtibot.mapper.UserMapper;
import com.zhi.mbtibot.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能:User类的方法具体实现，CRUD
 * 作者:YukiSu
 * 日期:2024/7/20 19:24
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public void addUser(User user) {
        int rows = userMapper.insert(user);
        if (rows == 1) {
            System.out.println("添加成功");
        }
        else {
            System.out.println("添加失败");
        }
    }

    @Override
    public void deleteUser(Integer uid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("uid", uid);
        int rows = userMapper.delete(queryWrapper);
        if (rows == 1) {
            System.out.println("删除成功");
        }
        else {
            System.out.println("删除失败");
        }
    }

    @Override
    public void updateUser(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("uid", user.getUid());
        int rows = userMapper.update(user, queryWrapper);
        if (rows == 1) {
            System.out.println("更新成功");
        }
        else {
            System.out.println("更新失败");
        }
    }

    @Override
    public User getUserById(Integer uid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("uid", uid);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User getUserByUserName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.selectList(null);
    }
}